True if any item in target equals value, false otherwise (including when target is empty). Nothing in target changes, and no item is taken off.
stack_contains(target, value) -> bool| Name | Type |
|---|---|
target | stack |
value | element |
bool
true if any item in target, a stack, equals value, false otherwise (including when target is empty).
It only looks. Nothing is taken off the stack and nothing is added, so the top item is still the top item afterwards. It answers yes or no and never says where the value is, because a stack has no places to count - only a top.
This is the stack's own name for the question list_contains asks a list, queue_contains asks a queue and set_contains asks a set. Each container uses its own name so it is always clear which kind of thing you are asking.
Numbers are equal when they are the same number, and text is equal when every letter is the same.
The search starts at the bottom and looks at one item after another until it finds a match, so a stack that holds more takes longer, and a search that finds nothing looks at every item. A stack that is only for pushing and popping never needs this; reach for it when you want to avoid pushing the same thing twice.
| You do this | What happens |
|---|---|
| search for a value that is not there | false |
| search for a value that is there more than once | true |
| search an empty stack | false |
| give it a list | refused when checked. A list has its own, list_contains |
/// @type {stack<int>}
var history = [];
stack_push(history, 4);
stack_push(history, 8);
var has8 = stack_contains(history, 8); // true
var has99 = stack_contains(history, 99); // false
if (not stack_contains(history, 12)) {
stack_push(history, 12); // only pushed if it is not already in the pile
}The same left-to-right search list_contains makes (find, the GBA module's own alias for std::find) over the vector the stack is kept in - O(n) in how many items target holds. It only answers yes or no; it does not say where the value is, because a stack has no places to count.